package com.lyf.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyf.comon.ResponseResult;
import com.lyf.entity.Admin;
import com.lyf.entity.SlideShow;
import com.lyf.exception.GlobalExceptionHandler;
import com.lyf.qo.SlideShowQuery;
import com.lyf.qo.UserQuery;
import com.lyf.service.SlideShowService;
import com.lyf.vo.SlideShowVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/slideShow")
public class SlideShowController {
    private final static Logger log = LoggerFactory.getLogger(SlideShowController.class);
    @Autowired
    private SlideShowService slideShowService;

    /**
     * 根据id查询轮播图
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/findSlideShowById")
    public ResponseResult findSlidShowById(@RequestParam Long id) throws Exception {
        SlideShow slidShowById = slideShowService.findSlidShowById(id);
        log.info("id{}",id);
        return ResponseResult.success(slidShowById);
    }

    /**
     * 条件分页查询
     * @param query
     * @return
     */
    @PostMapping("/listPage")
    @PreAuthorize("hasAuthority('system:slide:count')")
    public ResponseResult listPage(@RequestBody SlideShowQuery query) {
        try {
            IPage<SlideShow> result = slideShowService.listPage(query);
            if (result==null){
                return ResponseResult.success(Collections.emptyList());
            }
            return ResponseResult.success(result.getRecords(),result.getTotal());
        } catch (Exception e) {
            log.error("获取列表失败={}", e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }
    }

    /**
     * 根据类型id来查询轮播图
     * @param id
     * @return
     * @throws GlobalExceptionHandler
     */
    @GetMapping("/findByType")
    public ResponseResult findByType(@RequestParam Integer id) throws GlobalExceptionHandler {
        List<SlideShowVO> slideShowVOList = slideShowService.findByType(id);
        return ResponseResult.success(slideShowVOList);
    }

    /**
     * 新增或修改
     * @param slideShow
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @PreAuthorize("hasAuthority('system:slide:saveOrUpdateSlide')")
    public ResponseResult saveOrUpdateUser(@RequestBody SlideShow slideShow) {
        if (ObjectUtils.isEmpty(slideShow)) {
            return ResponseResult.fail("请传入参数");
        }
        try {
            slideShowService.saveOrUpdateSlideShow(slideShow);
            return ResponseResult.success();
        } catch (Exception | GlobalExceptionHandler e) {
            log.error("新增或修改失败={}", e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }
    }

    @GetMapping("/deleteById")
    @PreAuthorize("hasAuthority('system:slide:deleteById')")
    public ResponseResult deleteById(Long id) {
        try {
            Assert.notNull(id,"id不能为空");
            slideShowService.deleteById(id);
            return ResponseResult.success();
        } catch (Exception | GlobalExceptionHandler e) {
            log.error("删除用户失败={}", e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }
    }
}
